/******************************************************************************
 * @File name   :      CardDefinitionDaoImpl.java
 *
 * @Author      :      JIZHANGS
 *
 * @Date        :      Aug 11, 2015
 *
 * @Copyright Notice: 
 * Copyright (c) 2015 Capgemini, Inc. All  Rights Reserved.
 * This software is published under the terms of the Capgemini Software
 * License version 1.0, a copy of which has been included with this
 * distribution in the LICENSE.txt file.
 * 
 * 
 * ----------------------------------------------------------------------------
 * Date                   Who         Version        Comments
 * Aug 11, 2015 6:02:26 PM        JIZHANGS     1.0            Initial Version
 *****************************************************************************/
package com.cap.bts.crmdp.dealer.coupon.dao.impl;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;

import com.cap.bts.crmdp.dealer.coupon.dao.ICouponDao;
import com.cap.bts.crmdp.dealer.coupon.domain.Coupon;
import com.cap.bts.crmdp.dealer.coupon.dto.CouponForMARSDto;
import com.cap.bts.framework.common.dao.callback.NativeQueryBySqlJpaCallback;
import com.cap.bts.framework.common.dao.impl.BaseDaoImpl;
import com.cap.bts.framework.common.exception.AppException;
import com.cap.bts.framework.common.util.Utils;

/**
 *优惠券
 */
@Repository("couponDao")
public class CouponDaoImpl extends BaseDaoImpl<Coupon, Long> implements ICouponDao {

    /**
     * @Date        :      Aug 11, 2015
     * @param clazz
     */
    @Inject
    public CouponDaoImpl(@Value("com.cap.bts.crmdp.dealer.coupon.domain.Coupon") Class<Coupon> clazz) {
        super(clazz);
    }

    /**
     * {@inheritDoc} 
     * overridden:
     * @Date        :      Sep 15, 2015
     * @see com.cap.bts.crmdp.dealer.coupon.dao.ICouponDao#getCouponByCouponNumber(java.lang.String)
    **/
    @Override
    public Coupon getCouponByCouponNumber(String couponNumber) throws AppException {
        List<Coupon> list=super.getListByQueryWithVaParam("from Coupon a where a.couponNumber=?1 and a.redeemDate is null", couponNumber);
        if(list!=null && !list.isEmpty()){
            return super.findById(list.get(0).getId());
        }
        return null;
    }

    /**
     * {@inheritDoc} 
     * overridden:
     * @Date        :      Sep 15, 2015
     * @see com.cap.bts.crmdp.dealer.coupon.dao.ICouponDao#checkCouponExist(java.lang.String)
    **/
    @Override
    public Coupon checkCouponExist(String couponNumber) throws AppException {
        List<Coupon> list=super.getListByQueryWithVaParam("from Coupon a where a.couponNumber=?1 and a.redeemDate is not null", couponNumber);
        if(list!=null && !list.isEmpty()){
            return list.get(0);
        }
        return null;
    }

    /**
     * {@inheritDoc} 
     * overridden:
     * @Date        :      Sep 23, 2015
     * @see com.cap.bts.crmdp.dealer.coupon.dao.ICouponDao#getCouponListByBatchId(java.lang.Long)
    **/
    @Override
    public List<Coupon> getCouponListByBatchId(Long batchId,Long ticketId) throws AppException {
        return super.getListByQueryWithVaParam("from Coupon a where a.couponBatch.id=?1 and a.ticketDefId=?2",batchId,ticketId);
    }
    
    
    @Override
    public List<CouponForMARSDto> getCouponListByCouponNumber(String couponNumber) throws AppException {
    	List<CouponForMARSDto> list = new ArrayList<CouponForMARSDto>();
        String sql = "select  " +
    			" a.id " +
    			",a.coupon_batch_id " +
    			",a.ticket_def_id " +
    			",a.coupon_number " +
    			",b.ticket_NAME " +
    			",b.deduction_method " +
    			",b.deduction_amount " +
    			",b.ticket_type " +
    			",d.BATCH_NAME " +
    			",d.DESCRIPTION " +
    			",d.START_DATE " +
    			",d.END_DATE " +
    			",b.MAXIMUM_TIMES " +
    			",b.SMS_REMIND " +
    			" " +
    			" from tm_raw_coupon a, " +
    			" tm_raw_ticket_def b, " +
    			" tm_raw_coupon_batch d " +
    			" " +
    			" where  " +
    			" a.TICKET_DEF_ID = b.id " +
    			" and a.coupon_batch_id = d.id " +
    			" and a.COUPON_NUMBER = '"+couponNumber+"'";
        
        List<Object[]> dataList = getJpaTemplate().executeFind(
            new NativeQueryBySqlJpaCallback<List<Object[]>>(sql));
        
        if(dataList != null && !dataList.isEmpty()){
        	for (Object[] obj : dataList) {
        		CouponForMARSDto dto = new CouponForMARSDto();
        		
//        		dto.setCouponId(couponId);
        		dto.setCouponId(((BigInteger)obj[0]).intValue());
        		dto.setBatchId(((BigInteger)obj[1]).intValue());
        		dto.setTicketDefId(((BigInteger)obj[2]).intValue());
        		dto.setCouponNumber(Utils.getString(obj[3]));
        		dto.setTicketName(Utils.getString(obj[4]));
        		dto.setDeductionMethod(Utils.getString(obj[5]));
        		dto.setDeductionAmount(((Integer)obj[6]).intValue());
        		dto.setTicketType(Utils.getString(obj[7]));
        		dto.setBatchName(Utils.getString(obj[8]));
        		dto.setDescription(Utils.getString(obj[9]));
        		dto.setStartDate(Utils.getDate(obj[10]));
        		dto.setEndDate(Utils.getDate(obj[11]));
        		dto.setMaximunTimes((Integer)(obj[12]));
        		dto.setSmsRemind(Utils.getString(obj[13]));
            	
            	list.add(dto);
			}
        }
        
       
        return list;
    }
    
    
    /**
     * {@inheritDoc} 
     * overridden:
     * @Date        :      Sep 15, 2015
     * @see com.cap.bts.crmdp.dealer.coupon.dao.ICouponDao#getCouponByCouponNumber(java.lang.String)
    **/
    @Override
    public Coupon getCouponByCouponNo(String couponNumber) throws AppException {
        List<Coupon> list=super.getListByQueryWithVaParam("from Coupon a where a.couponNumber=?1 ", couponNumber);
        if(list!=null && !list.isEmpty()){
            return super.findById(list.get(0).getId());
        }
        return null;
    }
    

}
